
#include <stdio.h>
#include "cmsis_os2.h"                          // CMSIS RTOS header file
 
/*----------------------------------------------------------------------------
 *      Thread 1 'Thread_Name': Sample thread
 
 不在Thread_Disp()驱动DAC，因为显示频率低。更新DAC需要和ADC同步（同频率）。
 X名字：仿照Linux显示的X系统。
 *---------------------------------------------------------------------------*/
#include "dbgUart/dbgUart.h" 
#include "bsp.h"
#include "key.h"
#include "menu_seg.h"
#include "threads.h"
#include "parameter.h"
#include "preset_point.h"
#include "drv_ch423s.h"
#include "LED.h"


static void x_disp_pwr(void);

static void disp_X(void);

/*菜单项目
*/ 
void Thread_X (void *argument);                   // thread function
 

int Init_Thread_X (void) {
	osThreadId_t tid_Thread;                        // thread id
	
	const osThreadAttr_t attr={.priority= osPriorityNormal2,.stack_size= 512};	// thread id 
	tid_Thread = osThreadNew(Thread_X, NULL, &attr);
	if (tid_Thread == NULL) {
		return(-1);
	}
 
	return(0);
}


//extern int32_t offset_disp;		//显示时的偏移量。



__NO_RETURN void Thread_X (void *argument) {
	(void)argument;	//Prevent unused argument(s) compilation warning.
	
	char str[32];
	int32_t wt;
	osStatus_t ret;
	uint16_t key;				//从消息队列中获取的按键值。
	MSG_U msg;					//组合消息
	
	
	ch423s_init(2);
	
	for(int i= 0; i< 8; i++)
		disp_x.data_nc[i]= disp_x.data[i]= 0x0;
	
	disp_x.data_nc[7]= LED_ALL>>8;	//更新指示灯对应的寄存器。
	disp_x.data_nc[6]= LED_ALL;
	
	uartS("\nThread_X(N2) start...\n");
	
	
	x_disp_pwr();
	
	
	
	
	
//	//test
//	x_seg_flash_all(1);
//	
//	x_led_one_flash(0, DISP_X_MODE_FF);
//	x_led_one_flash(1, DISP_X_MODE_LF);
//	x_led_one_flash(2, DISP_X_MODE_LF);
//	x_led_one_flash(3, DISP_X_MODE_FF);
//	x_led_one_flash(4, DISP_X_MODE_FF);
//	x_led_one_flash(5, DISP_X_MODE_LF);
//	x_led_one_flash(6, DISP_X_MODE_FF);
//	x_led_one_flash(7, DISP_X_MODE_LF);
//	x_led_one_flash(8, DISP_X_MODE_LF);
	
	while(1){

		//LED_Screen_update();
		
		osDelay(500);		//闪烁的计时单位：50ms。
		uartS("X");
	}
}



/*所有数码管、指示灯亮。
*/
static void x_disp_8(void)
{
    ch423s_set(CMD_WR_DIG0, DAT_8, DOT_EN);		//显示小数点
    ch423s_set(CMD_WR_DIG1, DAT_8, DOT_EN);		//不显示小数点
    ch423s_set(CMD_WR_DIG2, DAT_8, DOT_EN);
    ch423s_set(CMD_WR_DIG3, DAT_8, DOT_EN);
    ch423s_set(CMD_WR_DIG4, DAT_8, DOT_EN);
    ch423s_set(CMD_WR_DIG5, DAT_8, DOT_EN);
	
    ch423s_set(CMD_WR_DIG6, DAT_8, DOT_EN);
}


/*所有数码管、指示灯亮。
*/
static void x_disp_blank(void)
{
    ch423s_set(CMD_WR_DIG0, DAT_BLANK, DOT_DIS);		//显示小数点
    ch423s_set(CMD_WR_DIG1, DAT_BLANK, DOT_DIS);		//不显示小数点
    ch423s_set(CMD_WR_DIG2, DAT_BLANK, DOT_DIS);
    ch423s_set(CMD_WR_DIG3, DAT_BLANK, DOT_DIS);
    ch423s_set(CMD_WR_DIG4, DAT_BLANK, DOT_DIS);
    ch423s_set(CMD_WR_DIG5, DAT_BLANK, DOT_DIS);
	
    ch423s_set(CMD_WR_DIG6, DAT_BLANK, DOT_DIS);
}



/*
#define DAT_F			0x71		//'F'
#define DAT_o			0x5C		//'o'
#define DAT_r			0x50		//'r'
#define DAT_C			0x39		//'C'
#define DAT_E			0x79		//'E'
*/
static void x_disp_logo(void)
{
    ch423s_set(CMD_WR_DIG0, DAT_0, DOT_DIS);		//显示小数点
    ch423s_set(CMD_WR_DIG1, DAT_F, DOT_DIS);		//不显示小数点
    ch423s_set(CMD_WR_DIG2, DAT_o, DOT_DIS);
    ch423s_set(CMD_WR_DIG3, DAT_r, DOT_DIS);
    ch423s_set(CMD_WR_DIG4, DAT_C, DOT_DIS);
    ch423s_set(CMD_WR_DIG5, DAT_E, DOT_DIS);
	
    ch423s_set(CMD_WR_DIG6, DAT_BLANK, DOT_DIS);
}



/*上电后的固定显示流程。
*/
static void x_disp_pwr(void)
{
	for(int i= 0; i< 3; i++){
		
		x_disp_8();
		osDelay(250);
		
		x_disp_blank();
		osDelay(250);
	}
	
	buz_beep(100);
	x_disp_logo();
	osDelay(1000);
}








